package com.example.controller;

import com.example.common.Result;
import com.example.entity.FeeRecord;
import com.example.entity.OrganizationMember;
import com.example.service.FeeService;
import com.example.service.OrganizationMemberService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/fee")
public class FeeController {
    
    @Autowired
    private FeeService feeService;

    @Resource
    private OrganizationMemberService memberService;
    
    @GetMapping("/page")
    public Result page(FeeRecord query,
                       @RequestParam Long userId,
                       @RequestParam Integer userType,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        // 根据用户类型设置查询条件
        if (userType == 4 || userType == 5 || userType == 3) {
            OrganizationMember member = memberService.getByUserId(userId);
            if (member != null) {
                query.setFeeType(member.getOrgType());
            }
        }
        query.setUserId(userId);
        PageInfo<FeeRecord> page = feeService.page(query, pageNum, pageSize);
        return Result.success(page);
    }
    
    @PostMapping("/publish")
    public Result publish(@RequestBody FeeRecord record) {
        feeService.publish(record);
        return Result.success();
    }
    
    @PostMapping("/pay/{recordId}")
    public Result pay(@PathVariable Long recordId,
                     @RequestParam Long userId,
                     @RequestParam Integer paymentMethod) {
        feeService.pay(recordId, userId, paymentMethod);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable int id) {
        feeService.delete(id);
        return Result.success();
    }
}
